/*
 * ImportDialog.cs - Wordtrainer
 * 
 * Copyright (C) 1998-2007 Markus Kilås
 *               
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 *
 * Authors:
 *   Markus Kilås <markus@markuspage.com>
 *
 */

using System;
using System.Collections;
using Gtk;

namespace WtSharp
{
	
	
	public partial class ImportDialog : Gtk.Dialog
	{
		private ArrayList m_Words;
		private ArrayList m_SelectedWords = null;
		private Gdk.Pixbuf pixbufMarkS;
		private Gdk.Pixbuf pixbufMarkV;
		
		public ImportDialog(Gtk.Window parent, ArrayList words, string fileName) : base("Import words", parent, Gtk.DialogFlags.Modal)
		{
			this.Build();
			m_Words = words;
			
			treeview1.AppendColumn("Type", new CellRendererPixbuf(), "pixbuf", 1);
			treeview1.AppendColumn("Foreign word", new CellRendererText(), "text", 2);
			treeview1.AppendColumn("Translation", new CellRendererText(), "text", 3);
			treeview1.ColumnsAutosize();

			treeview1.Selection.Mode = SelectionMode.Multiple;

			// Load marks
			pixbufMarkS = Gdk.Pixbuf.LoadFromResource("mark-s.png").ScaleSimple(16,16,Gdk.InterpType.Bilinear);
			pixbufMarkV = Gdk.Pixbuf.LoadFromResource("mark-v.png").ScaleSimple(16,16,Gdk.InterpType.Bilinear);
			
			labelFilename.Text = fileName;
			
			populateList(words);
		}
		
		private void populateList(ArrayList words)
		{
			TreeStore store = new TreeStore (typeof (WtWord), typeof (Gdk.Pixbuf), typeof(string), typeof(string));
			
			Gdk.Pixbuf pixbuff = null;
			foreach(WtWord word in words)
		    {
				if(word.Type == 1)
					pixbuff = pixbufMarkS;
				else if(word.Type == 2)
					pixbuff = pixbufMarkV;
				else
					pixbuff = null;
				store.AppendValues (word, pixbuff, word.WordF, word.WordT);
		    }
			treeview1.Model = store;
		}

		protected virtual void OnButtonOkClicked (object sender, System.EventArgs e)
		{
			m_SelectedWords = new ArrayList();
			foreach(TreePath path in treeview1.Selection.GetSelectedRows())
			{
				foreach(int index in path.Indices)
				{
					m_SelectedWords.Add(m_Words[index]);
				}
			}
			Destroy();
		}

		protected virtual void OnButtonCancelClicked (object sender, System.EventArgs e)
		{
			Destroy();
		}
		
		public ArrayList SelectedWords
		{
			get
			{
				return m_SelectedWords;
			}
		}
	}
}
